/*
  系统相关
*/
import { isFunction } from 'lodash-es'
const userAgent = navigator.userAgent

/**
 * @function  Mac
 * @returns   { Boolean }  true：是，false 否
 * @example   systemMac()
 */
export const systemMac = (): boolean => /macintosh|mac os x/i.test(userAgent)

/**
 * @function Android
 * @returns  { Boolean } true：是，false 否
 * @example   systemAndroid()
 */
export const systemAndroid = (): boolean => /(Android)/i.test(userAgent)

/**
 * @function  window
 * @returns   { Boolean } true：是，false 否
 * @example   systemWindows()
 */
export const systemWindows = (): boolean => /windows|win32/i.test(userAgent)

/**
 * @function  h5端
 * @returns   { Boolean } true：是，false 否
 * @example   systemH5()
 */
export const systemH5 = (): boolean => {
  try {
    document.createEvent('TouchEvent')
    return true
  } catch (e) {
    return false
  }
}

/**
 * @function  浏览器内核
 * @returns   { String | undefined } Trident|Gecko|WebKitOrBlink|Presto
 * @example   getKernel()
 */
export function getKernel(): string | undefined {
  const trident = /Trident\/[\d.]+/
  const gecko = /Gecko\/\d+/
  const webKit = /WebKit\/[\d.]+/
  const presto = /Presto\/[\d.]+/
  // IE
  if (trident.test(userAgent)) {
    return 'Trident'
  }
  // 火狐
  if (gecko.test(userAgent)) {
    return 'Gecko'
  }
  // 谷歌/360极速/苹果/Microsoft Edge 等浏览器
  if (webKit.test(userAgent)) {
    return 'WebKitOrBlink'
  }
  // opera
  if (presto.test(userAgent)) {
    return 'Presto'
  }

  return
  const isOpera = userAgent.indexOf('Opera') > -1
  const isEdge = userAgent.indexOf('Edg') > -1
  const isFF = userAgent.indexOf('Firefox') > -1
  const isSafari = userAgent.indexOf('Safari') > -1 && userAgent.indexOf('Chrome') === -1
  const isChrome = userAgent.indexOf('Chrome') > -1 && userAgent.indexOf('Safari') > -1
  if (userAgent.indexOf('DingTalk') > -1) {
    return 'DingTalk'
  }
  if (userAgent.toLowerCase().indexOf('wxwork') > -1) {
    return 'WeCom'
  }
  if (userAgent.toLowerCase().indexOf('micromessenger') > -1) {
    return 'WeChat'
  }
  // 正常浏览器放到最后判断
  if (!!(window as any).ActiveXObject || 'ActiveXObject' in window) {
    return 'IE'
  }
  if (isOpera) {
    return 'Opera'
  }
  if (isEdge) {
    return 'Edge'
  }
  if (isFF) {
    return 'Firefox'
  }
  if (isSafari) {
    return 'Safari'
  }
  if (isChrome) {
    return 'Chrome'
  }
}

/**
 * @function  H5页面打开的APP类型
 * @returns   { String | undefined } WeCom|WeChat|DingTalk|QQBrowser|Alipay|Safari
 * @example   webAppEnvironment()
 */
export function webAppEnvironment(): string | undefined {
  // 企业微信
  if (userAgent.toLowerCase().indexOf('wxwork') > -1) {
    return 'WeCom'
  }
  // 微信
  if (userAgent.toLowerCase().indexOf('micromessenger') > -1) {
    return 'WeChat'
  }
  // 钉钉
  if (userAgent.indexOf('DingTalk') > -1) {
    return 'DingTalk'
  }
  // qq
  if (userAgent.indexOf('QQBrowser') > -1) {
    return 'QQBrowser'
  }
  // 支付宝
  if (userAgent.indexOf('AlipayClient') > -1) {
    return 'Alipay'
  }
  // Safari
  if (
    /iPhone|iPad|iPod/.test(userAgent) &&
    /AppleWebKit/.test(userAgent) &&
    !/CriOS/.test(userAgent) &&
    !/FxiOS/.test(userAgent)
  ) {
    return 'Safari'
  }
}

/**
 * @function  存储localStorage
 * @param     { String } name 存储localStorage的名称
 * @param     { * }      value 存储localStorage的值
 * @returns   { undefined }
 * @example   setLocalStorage('token', '0x001')
 */
export function setLocalStorage(name: string, value: any): void {
  let res: any = isFunction(value) ? value : JSON.stringify(value)
  window.localStorage.setItem(name, res)
}

/**
 * @function   获取localStorage
 * @param     { String }  name 存储localStorage的名称
 * @returns   { * }       存储的值
 * @example   getLocalStorage('token')
 */
export function getLocalStorage(name: string): any {
  const res: any = window.localStorage.getItem(name) ?? ''
  try {
    return JSON.parse(res)
  } catch (e) {
    return res
  }
}

/**
 * @function  删除localStorage
 * @param     { String }    name 删除localStorage的名称
 * @returns   { undefined }
 * @example   removeLocalStorage('token')
 */
export function removeLocalStorage(name: string): void {
  window.localStorage.removeItem(name)
}

/**
 * @function  清空localStorage
 * @returns   { undefined }
 * @example   clearLocalStorage()
 */
export function clearLocalStorage(): void {
  window.localStorage.clear()
}

/**
 * @function  存储sessionStorage
 * @param     { String } name   存储sessionStorage的名称
 * @param     { * }      value  存储sessionStorage的值
 * @returns   { undefined }
 * @example   setSessionStorage('token', '0x001')
 */
export function setSessionStorage(name: string, value: any): void {
  let res: any = isFunction(value) ? value : JSON.stringify(value)
  window.sessionStorage.setItem(name, res)
}

/**
 * @function  获取sessionStorage
 * @param     { String }  name 存储sessionStorage的名称
 * @returns   { * }       存储的值
 * @example   getSessionStorage('token')
 */
export function getSessionStorage(name: string): any {
  const res: any = window.sessionStorage.getItem(name) ?? ''
  try {
    return JSON.parse(res)
  } catch (e) {
    return res
  }
}

/**
 * @function  删除sessionStorage
 * @param     { String }    name 删除sessionStorage的名称
 * @returns   { undefined }
 * @example   removeSessionStorage('token')
 */
export function removeSessionStorage(name: string): void {
  window.sessionStorage.removeItem(name)
}

/**
 * @function  清空sessionStorage
 * @returns   { undefined }
 * @example   clearSessionStorage()
 */
export function clearSessionStorage(): void {
  window.sessionStorage.clear()
}
